import java.io.*;

import java.util.ArrayList;

public class testMenu
{
     public static void main (String args[]) throws Exception
     {
          //call the menu function and display the returned menu shortcut key
          char key;
          key=Menu();
          System.out.println(key);
     }
     
     
     public static char Menu() throws Exception
     { 
          //declare global variables
          char reply='0'; 
          String promptMenu="Please select a menu\n";
          promptMenu+="Enter full menu name or just the shortcut key";
          String promptCommand="Please select one of the following options\n";
          promptCommand+="Enter the number only";
          BufferedReader scan = new BufferedReader(new InputStreamReader(System.in) );
          menu current;
          String response="hi";
          int option;
          
          //initialize a dummy menu
          menu top = new menu("top menu",null,'t');
          current=top;
          top.addItem(new menu("File","Standard File Menu",top,'f'));
          top.addItem(new menu("Help","Standard Help Menu",top,'h'));
          //end of initialization
                 
          //display current menu and options
          while(!response.equalsIgnoreCase("done"))
          {
               option=-1;
               current.printMenu();
               System.out.println(promptCommand);
               current.displayOptions();
               
               //prompt user to select an option ane execute it
               //continue prompting until user input is valid
               while(option==-1)
               {
                    //a valid input must be an integer
                    try
                    {
                         option=Integer.parseInt((scan.readLine()).trim());
                    }
                    catch (Exception e)
                    {
                         System.out.println("invalid selection, please enter a digit only");
                    }
               }
               //interpret user's selection
               response=current.interpret(option);
          
               //depending on the response, act accordingly
               //switch to submenu
               if (response.equalsIgnoreCase("sub"))
               {
                    //display the submenu and prompt user to choose 
                    System.out.println(promptMenu);
                    String selection=(scan.readLine()).trim();          
                    int index;
                    //user inputs a shortcut key
                    if (selection.length()==1)
                    {
                         menu temp=current.getItem(selection.charAt(0));
                         if (temp!=null)
                         current=temp;
                    }
                    //user inputs a menu title
                    else
                    {
                         menu temp=current.getItem(selection);
                         if (temp!=null)
                         current=temp;
                    }
               }
               //modify user prompt
               if (response.equalsIgnoreCase("userPrompt"))
               {
                    System.out.println("Please enter the new prompt");
                    promptCommand=scan.readLine();
               }
               //return to parent menu
               if (response.equalsIgnoreCase("parent"))
               {
                    current=current.getParent();
               }
               //exit the program
               if (response.equalsIgnoreCase("exit"))
               {
                    return '0';
               }    
          }
          
          reply=current.getKey();
          return reply;
     }
}